using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI.SmartParts;
using Reflector.CodeModel;
namespace Deblector.WorkItems
{
    [SmartPart]
    public partial class TextView : UserControl,IFormatter
    {
        public TextView()
        {
            InitializeComponent();
        }
        public void Clear()
        {
            richTextBox1.Clear();
        }
        private bool allowProperties = false;
        private bool newLine = false;
        private int indent = 0;
        public void Write(string text)
		{
			this.ApplyIndent();
            this.ApplyIndent();
            richTextBox1.Select(richTextBox1.Text.Length, 0);
            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Regular);
            richTextBox1.SelectionColor = Color.Black;
            Append(text);
		}
	
		public void WriteDeclaration(string text)
		{
			this.WriteBold(text);	
		}
		
		public void WriteComment(string text)
		{
			this.WriteColor(text, (int) 0x808080);	
		}
		
		public void WriteLiteral(string text)
		{
			this.WriteColor(text, (int) 0x800000);
		}
		
		public void WriteKeyword(string text)
		{
			this.WriteColor(text, (int) 0x000080);
		}
	
		public void WriteIndent()
		{
			this.indent++;
		}
				
		public void WriteLine()
		{
            Append("\n");
			this.newLine = true;
		}

		public void WriteOutdent()
		{
			this.indent--;
		}

		public void WriteReference(string text, string toolTip, Object reference)
		{
			this.ApplyIndent();
			Append(text);
		}

		public void WriteProperty(string propertyName, string propertyValue)
		{
			if (this.allowProperties)
			{
				throw new NotSupportedException();
			}
		}

		public bool AllowProperties
		{
			set 
			{ 
				this.allowProperties = value; 
			}
			
			get 
			{ 
				return this.allowProperties;
			}
		}
        private void Append(string Text)
        {
            
            richTextBox1.SelectedText = Text;
        }
		private void WriteBold(string text)
		{
			this.ApplyIndent();
            richTextBox1.Select(richTextBox1.Text.Length, 0);
            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold);
            richTextBox1.SelectionColor = Color.Black;
            Append(text);
           
		}

		private void WriteColor(string text, int color)
		{
			this.ApplyIndent();
            richTextBox1.Select(richTextBox1.Text.Length, 0);
            richTextBox1.SelectionColor = Color.FromArgb(color);
            Append(text);
		}

		private void ApplyIndent()
		{
			if (this.newLine)
			{
				for (int i = 0; i < this.indent; i++)
				{
                    Append("    ");
				}

				this.newLine = false;
			}
		}

        #region IFormatter Members


        public void WriteDeclaration(string value, object target)
        {
            Append(value);
        }

        #endregion
    }
}
